<?php
namespace app\common\server;
use x\Xbase;
class UserUpgrade extends Xbase
{

    /**
     * 余额支付   升级vip等级之上（零售，批发，代理），获得推广，招商奖
     * @param  [type] $order_no [description]
     * @return [type]           [description]
     */
    public function pay($order_no){
        //查询订单
        $map = [];
        $map[] = ["and", "order_no", "=", $order_no];
        $map[] = ["and", "pay_status", "=", 0];
        $w = where($map);
        $order_info = $this->db->query("select * from user_upgrade $w[0]", $w[1],1);
        if (empty($order_info)) return $this->setError('订单不存在!');

        $this->db->begin();
        $mysql = [];
        //修改级别 - 库存
        $mysql[] = $this->db->query("update user set level=?,vip_sum_nums=vip_sum_nums+?,vip_nums=vip_nums+? where id=?",[$order_info["after_level"],$order_info['stock'],$order_info['stock'],$order_info['user_id']]);
        #库存log
        $this->db->insert('user_vip_nums_log',[
                        'user_id' => $order_info['user_id'],
                        'order_upgrade_id' => $order_info['id'],
                        'nums' => $order_info['stock'],
                        'create_time' => time()]);
        //升级扣钱
        $mysql[] = m('Userbill', 'xcom')->doit($order_no, $order_info['user_id'], 203, 'user_upgrade', $order_info['id'], $order_info['money'],'money','升级', 0);
        #升级赠送积分
        if ($order_info['points']>0) {
            $com_order_no = $this->com_order_no();
            $mysql[] = $order_id = m('Orderjifen','c')->add($com_order_no,$order_info['user_id'],$order_info['points'],8);
            $mysql[] =m('Userbill','xcom')->doit($com_order_no,$order_info['user_id'],105,'order_jifen',$order_id,$order_info['points'],'money_jf','');
        }

        //------升级奖金逻辑--------------------------------------------
        //总的钱
        $total_money = $order_info['money'];
        $total_stock = $order_info['stock'];
        #获取上级每个级别第一人
        $father = m('User','c')->myfirstfather($order_info['user_id']);
        $father = array_filter($father);
        #修改订单支付状态及奖金记录
        $temp = [
            'id' => $order_info['id'],
            'pay_status' => 1,
            'pay_time' => time(),
            'create_time' => time(),
        ];

        #---第一个人-----------------------------------------------------------
        $first_person = reset($father);

        $is_second = false;
        #升级同级别 获得推广奖
        if($first_person){
            if($first_person['level']==$order_info['after_level']){
                $data = [
                    'order_no' => $this->com_order_no(),
                    'order_upgrade_id' => $order_info['id'],
                    'user_id' => $first_person['id'],
                    'level' => $first_person['level'],
                    'money' => sys("tuiguang_money_{$first_person['level']}"),
                    'status' => 1,
                    'create_time' => time(),
                ];
                $data['real_money'] = $data['money'];
                $data['note'] = '获得推广奖';
                $mysql[] = $id = $this->db->insert('award_tuiguang',$data);
                #算奖
                if ($data['real_money']>0) {
                    $mysql[] = m('Userbill','xcom')->doit($data['order_no'],$data['user_id'],110,'award_tuiguang',$id,$data['real_money'],'money_sy','获得推广奖');
                }
                #记录
                $temp["level{$first_person['level']}_user_id"]     = $first_person['id'];
                $temp["level{$first_person['level']}_befor_stock"] = $first_person['vip_nums'];
                $temp["level{$first_person['level']}_after_stock"] = $first_person['vip_nums'];
                $temp["level{$first_person['level']}_money"]       = $data['money'];
                #剩余金额
                $total_money = $total_money - $data['money'];

                $is_second = true;
            }else{
                #不同级别，有库存，获得total_money
                if($first_person['vip_nums']>$total_stock){
                    $data = [
                        'order_no' => $this->com_order_no(),
                        'user_upgrade_id' => $order_info['id'],
                        'user_id' => $first_person['id'],
                        'level' => $first_person['level'],
                        'money' => $total_money,
                        'status' => 1,
                        'create_time' => time(),
                    ];
                    $data['real_money'] = $data['money'];
                    $data['note'] = '获得招商奖';
                    $mysql[] = $id = $this->db->insert('award_zhaoshang',$data);
                    #算奖
                    if ($data['real_money']>0) {
                        $mysql[] = m('Userbill','xcom')->doit($data['order_no'],$data['user_id'],109,'award_zhaoshang',$id,$data['real_money'],'money_sy','获得招商奖');
                    }
                    #记录
                    $temp["level{$first_person['level']}_user_id"]     = $first_person['id'];
                    $temp["level{$first_person['level']}_befor_stock"] = $first_person['vip_nums'];
                    $temp["level{$first_person['level']}_after_stock"] = $first_person['vip_nums']-$total_stock;
                    $temp["level{$first_person['level']}_money"]       = $data['money'];
                    #剩余金额
                    $total_money = $total_money - $data['money'];
                    #扣除库存
                    $this->db->update('user',['id'=>$first_person['id'],'vip_nums'=>$first_person['vip_nums']-$total_stock]);
                    #库存log
                    $this->db->insert('user_vip_nums_log',[
                                    'user_id' => $first_person['id'],
                                    'order_upgrade_id' => $order_info['id'],
                                    'nums' => $total_stock,
                                    'create_time' => time()]);
                    #剩余库存
                    $total_stock = 0;

                }else if($first_person['vip_nums']>0){
                #不同级别，部分库存，剩余金额-进货库存数量*进货价
                    $cha_stock = $total_stock - $first_person['vip_nums'];
                    $data = [
                        'order_no' => $this->com_order_no(),
                        'user_upgrade_id' => $order_info['id'],
                        'user_id' => $first_person['id'],
                        'level' => $first_person['level'],
                        'money' => $total_money-($cha_stock*c("rate.stock_price")[$first_person['level']]['buy']),
                        'status' => 1,
                        'create_time' => time(),
                    ];
                    $data['real_money'] = $data['money'];
                    $data['note'] = '获得招商奖';
                    $mysql[] = $id = $this->db->insert('award_zhaoshang',$data);
                    #算奖
                    if ($data['real_money']>0) {
                        $mysql[] = m('Userbill','xcom')->doit($data['order_no'],$data['user_id'],109,'award_zhaoshang',$id,$data['real_money'],'money_sy','获得招商奖');
                    }
                    #记录
                    $temp["level{$first_person['level']}_user_id"]     = $first_person['id'];
                    $temp["level{$first_person['level']}_befor_stock"] = $first_person['vip_nums'];
                    $temp["level{$first_person['level']}_after_stock"] = 0;
                    $temp["level{$first_person['level']}_money"]       = $data['money'];
                    #剩余金额
                    $total_money = $total_money - $data['money'];
                    #扣除库存
                    $this->db->update('user',['id'=>$first_person['id'],'vip_nums'=>0]);
                    #库存log
                    $this->db->insert('user_vip_nums_log',[
                                    'user_id' => $first_person['id'],
                                    'order_upgrade_id' => $order_info['id'],
                                    'nums' => $first_person['vip_nums'],
                                    'create_time' => time()]);
                    #剩余库存
                    $total_stock = $total_stock-$first_person['vip_nums'];

                    $is_second = true;
                }else{
                #不同级别，无库存，获得剩余金额-库存数量*进货价
                    $data = [
                        'order_no' => $this->com_order_no(),
                        'user_upgrade_id' => $order_info['id'],
                        'user_id' => $first_person['id'],
                        'level' => $first_person['level'],
                        'money' => $total_money-($total_stock*c("rate.stock_price")[$first_person['level']]['buy']),
                        'status' => 1,
                        'create_time' => time(),
                    ];
                    $data['real_money'] = $data['money'];
                    $data['note'] = '获得招商奖';
                    $mysql[] = $id = $this->db->insert('award_zhaoshang',$data);
                    #算奖
                    if ($data['real_money']>0) {
                        $mysql[] = m('Userbill','xcom')->doit($data['order_no'],$data['user_id'],109,'award_zhaoshang',$id,$data['real_money'],'money_sy','获得招商奖');
                    }
                    #记录
                    $temp["level{$first_person['level']}_user_id"]     = $first_person['id'];
                    $temp["level{$first_person['level']}_befor_stock"] = $first_person['vip_nums'];
                    $temp["level{$first_person['level']}_after_stock"] = $first_person['vip_nums'];
                    $temp["level{$first_person['level']}_money"]       = $data['money'];
                    #剩余金额
                    $total_money = $total_money - $data['money'];
                    
                    $is_second = true;
                }
            }
        }

        #---第二个人-----------------------------------------------------------
        array_shift($father);
        $second_person = reset($father);

        $is_three = false;
        if($is_second==true && $second_person){
            #有库存，获得剩余金额
            if($second_person['vip_nums']>$total_stock){
                $data = [
                    'order_no' => $this->com_order_no(),
                    'user_upgrade_id' => $order_info['id'],
                    'user_id' => $second_person['id'],
                    'level' => $second_person['level'],
                    'money' => $total_money,
                    'status' => 1,
                    'create_time' => time(),
                ];
                $data['real_money'] = $data['money'];
                $data['note'] = '获得招商奖';
                $mysql[] = $id = $this->db->insert('award_zhaoshang',$data);
                #算奖
                if ($data['real_money']>0) {
                    $mysql[] = m('Userbill','xcom')->doit($data['order_no'],$data['user_id'],109,'award_zhaoshang',$id,$data['real_money'],'money_sy','获得招商奖');
                }
                #记录
                $temp["level{$second_person['level']}_user_id"]     = $second_person['id'];
                $temp["level{$second_person['level']}_befor_stock"] = $second_person['vip_nums'];
                $temp["level{$second_person['level']}_after_stock"] = $second_person['vip_nums']-$total_stock;
                $temp["level{$second_person['level']}_money"]       = $data['money'];
                #剩余金额
                $total_money = $total_money - $data['money'];
                #扣除库存
                $this->db->update('user',['id'=>$second_person['id'],'vip_nums'=>$second_person['vip_nums']-$total_stock]);
                #库存log
                $this->db->insert('user_vip_nums_log',[
                                'user_id' => $second_person['id'],
                                'order_upgrade_id' => $order_info['id'],
                                'nums' => $total_stock,
                                'create_time' => time()]);
                #剩余库存
                $total_stock = 0;
            }else if($second_person['vip_nums']>0){
            #部分库存，剩余金额-进货库存数量*进货价
                $cha_stock = $total_stock - $second_person['vip_nums'];
                $data = [
                    'order_no' => $this->com_order_no(),
                    'user_upgrade_id' => $order_info['id'],
                    'user_id' => $second_person['id'],
                    'level' => $second_person['level'],
                    'money' => $total_money-($cha_stock*c("rate.stock_price")[$second_person['level']]['buy']),
                    'status' => 1,
                    'create_time' => time(),
                ];
                $data['real_money'] = $data['money'];
                $data['note'] = '获得招商奖';
                $mysql[] = $id = $this->db->insert('award_zhaoshang',$data);
                #算奖
                if ($data['real_money']>0) {
                    $mysql[] = m('Userbill','xcom')->doit($data['order_no'],$data['user_id'],109,'award_zhaoshang',$id,$data['real_money'],'money_sy','获得招商奖');
                }
                #记录
                $temp["level{$second_person['level']}_user_id"]     = $second_person['id'];
                $temp["level{$second_person['level']}_befor_stock"] = $second_person['vip_nums'];
                $temp["level{$second_person['level']}_after_stock"] = 0;
                $temp["level{$second_person['level']}_money"]       = $data['money'];
                #剩余金额
                $total_money = $total_money - $data['money'];
                #扣除库存
                $this->db->update('user',['id'=>$second_person['id'],'vip_nums'=>0]);
                #库存log
                $this->db->insert('user_vip_nums_log',[
                                'user_id' => $second_person['id'],
                                'order_upgrade_id' => $order_info['id'],
                                'nums' => $second_person['vip_nums'],
                                'create_time' => time()]);
                #剩余库存
                $total_stock = $total_stock-$second_person['vip_nums'];

                $is_three = true;
            }else{
            #无库存，获得剩余金额-库存数量*进货价
                $data = [
                    'order_no' => $this->com_order_no(),
                    'user_upgrade_id' => $order_info['id'],
                    'user_id' => $second_person['id'],
                    'level' => $second_person['level'],
                    'money' => $total_money-($total_stock*c("rate.stock_price")[$second_person['level']]['buy']),
                    'status' => 1,
                    'create_time' => time(),
                ];
                $data['real_money'] = $data['money'];
                $data['note'] = '获得招商奖';
                $mysql[] = $id = $this->db->insert('award_zhaoshang',$data);
                #算奖
                if ($data['real_money']>0) {
                    $mysql[] = m('Userbill','xcom')->doit($data['order_no'],$data['user_id'],109,'award_zhaoshang',$id,$data['real_money'],'money_sy','获得招商奖');
                }
                #记录
                $temp["level{$second_person['level']}_user_id"]     = $second_person['id'];
                $temp["level{$second_person['level']}_befor_stock"] = $second_person['vip_nums'];
                $temp["level{$second_person['level']}_after_stock"] = $second_person['vip_nums'];
                $temp["level{$second_person['level']}_money"]       = $data['money'];
                #剩余金额
                $total_money = $total_money - $data['money'];

                $is_three = true;
            }
        }

        #---第三个人-----------------------------------------------------------
        array_shift($father);
        $three_person = reset($father);

        if($is_three==true && $three_person){
            #有库存，获得剩余金额
            if($three_person['vip_nums']>$total_stock){
                $data = [
                    'order_no' => $this->com_order_no(),
                    'user_upgrade_id' => $order_info['id'],
                    'user_id' => $three_person['id'],
                    'level' => $three_person['level'],
                    'money' => $total_money,
                    'status' => 1,
                    'create_time' => time(),
                ];
                $data['real_money'] = $data['money'];
                $data['note'] = '获得招商奖';
                $mysql[] = $id = $this->db->insert('award_zhaoshang',$data);
                #算奖
                if ($data['real_money']>0) {
                    $mysql[] = m('Userbill','xcom')->doit($data['order_no'],$data['user_id'],109,'award_zhaoshang',$id,$data['real_money'],'money_sy','获得招商奖');
                }
                #记录
                $temp["level{$three_person['level']}_user_id"]     = $three_person['id'];
                $temp["level{$three_person['level']}_befor_stock"] = $three_person['vip_nums'];
                $temp["level{$three_person['level']}_after_stock"] = $three_person['vip_nums']-$total_stock;
                $temp["level{$three_person['level']}_money"]       = $data['money'];
                #剩余金额
                $total_money = $total_money - $data['money'];
                #扣除库存
                $this->db->update('user',['id'=>$three_person['id'],'vip_nums'=>$three_person['vip_nums']-$total_stock]);
                #库存log
                $this->db->insert('user_vip_nums_log',[
                                'user_id' => $three_person['id'],
                                'order_upgrade_id' => $order_info['id'],
                                'nums' => $total_stock,
                                'create_time' => time()]);
                #剩余库存
                $total_stock = 0;
            }else if($three_person['vip_nums']>0){
            #部分库存，剩余金额-进货库存数量*进货价
                $cha_stock = $total_stock - $three_person['vip_nums'];
                $data = [
                    'order_no' => $this->com_order_no(),
                    'user_upgrade_id' => $order_info['id'],
                    'user_id' => $three_person['id'],
                    'level' => $three_person['level'],
                    'money' => $total_money-($cha_stock*c("rate.stock_price")[$three_person['level']]['buy']),
                    'status' => 1,
                    'create_time' => time(),
                ];
                $data['real_money'] = $data['money'];
                $data['note'] = '获得招商奖';
                $mysql[] = $id = $this->db->insert('award_zhaoshang',$data);
                #算奖
                if ($data['real_money']>0) {
                    $mysql[] = m('Userbill','xcom')->doit($data['order_no'],$data['user_id'],109,'award_zhaoshang',$id,$data['real_money'],'money_sy','获得招商奖');
                }
                #记录
                $temp["level{$three_person['level']}_user_id"]     = $three_person['id'];
                $temp["level{$three_person['level']}_befor_stock"] = $three_person['vip_nums'];
                $temp["level{$three_person['level']}_after_stock"] = 0;
                $temp["level{$three_person['level']}_money"]       = $data['money'];
                #剩余金额
                $total_money = $total_money - $data['money'];
                #扣除库存
                $this->db->update('user',['id'=>$three_person['id'],'vip_nums'=>0]);
                #库存log
                $this->db->insert('user_vip_nums_log',[
                                'user_id' => $three_person['id'],
                                'order_upgrade_id' => $order_info['id'],
                                'nums' => $three_person['vip_nums'],
                                'create_time' => time()]);
                #剩余库存
                $total_stock = $total_stock-$three_person['vip_nums'];
            }else{
            #无库存，获得剩余金额-剩余库存*进货价
                $data = [
                    'order_no' => $this->com_order_no(),
                    'user_upgrade_id' => $order_info['id'],
                    'user_id' => $three_person['id'],
                    'level' => $three_person['level'],
                    'money' => $total_money-($total_stock*c("rate.stock_price")[$three_person['level']]['buy']),
                    'status' => 1,
                    'create_time' => time(),
                ];
                $data['real_money'] = $data['money'];
                $data['note'] = '获得招商奖';
                $mysql[] = $id = $this->db->insert('award_zhaoshang',$data);
                #算奖
                if ($data['real_money']>0) {
                    $mysql[] = m('Userbill','xcom')->doit($data['order_no'],$data['user_id'],109,'award_zhaoshang',$id,$data['real_money'],'money_sy','获得招商奖');
                }
                #记录
                $temp["level{$three_person['level']}_user_id"]     = $three_person['id'];
                $temp["level{$three_person['level']}_befor_stock"] = $three_person['vip_nums'];
                $temp["level{$three_person['level']}_after_stock"] = $three_person['vip_nums'];
                $temp["level{$three_person['level']}_money"]       = $data['money'];
                #剩余金额
                $total_money = $total_money - $data['money'];
            }
        }

        $mysql[] = $this->db->update("user_upgrade", $temp);
        $rs = $this->db->end($mysql);
        if (!$rs) {
            return $this->setError('支付失败!');
        }
        return true;
    }

}
